package com.fresh.controller;

import com.fresh.constant.JwtClaimsConstant;
import com.fresh.dto.UserLoginDTO;
import com.fresh.dto.UserPageQueryDTO;
import com.fresh.dto.UserRegisterDTO;
import com.fresh.dto.UserUpdateDTO;
import com.fresh.pojo.User;
import com.fresh.properties.JwtProperties;
import com.fresh.result.PageResult;
import com.fresh.result.Result;
import com.fresh.service.UserService;
import com.fresh.util.JwtUtil;
import com.fresh.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Long
 * @title UserController
 * @date 2024/07/01 19:09
 * @description TODO
 */

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录{}", userLoginDTO);
        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }


    /**
     * 用户注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("用户注册{}", userRegisterDTO);
        userService.register(userRegisterDTO);
        return Result.success();
    }

    /**
     * 用户分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "用户分页查询")
    public Result<PageResult> pageQuery(UserPageQueryDTO userPageQueryDTO) {
        log.info("员工分页查询");
        PageResult pageResult = userService.query(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改员工数据
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改员工数据")
    public Result update(@RequestBody UserUpdateDTO userUpdateDTO) {
        log.info("修改用户数据{}", userUpdateDTO);
        userService.update(userUpdateDTO);
        return Result.success();
    }

    /**
     * 修改用户状态
     * @param status
     * @return
     */
    @PostMapping("/updateStatus/{status}")
    @ApiOperation(value = "修改用户状态")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        if(status == 1L || status == 0L) {
            log.info("修改用户状态{}, {}", status, id);
            userService.updateStatus(status, id);
            return Result.success();
        } else {
            return Result.error("状态码输入有误！");
        }
    }

}
